<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title></title>
</head>
<body>
	<h1 style="color:red;text-align:center">联系人导出工具</h1>
	<div style='padding:10px' id='list'></div>	
	
	<div style='text-align:center;margin:50px'>
		<button id='GetContactsList'>获取联系人列表</button>
	</div>
	<div style='text-align:center;margin:50px'>
		<button id='DownFiles'>导出</button>
	</div>

	<div style='padding:10px;word-break:break-all;color:red;font-size:18px' id='Notice'></div>	
</body>
</html>
<script type="text/javascript">
	var tmp_str = '';   //用于显示html内容的字串
	var tmp_str2 = '';  //用于存储写入文件的字串
	var currnet_dir = '';  //保存文件的目录路径
	var current_file = ''; //保存文件的完整路径
	
	
	/*
	 	### 创建文件并写入内容
	 	参一：path_root string 根目录 ,如 "_doc"
	 	参二：path_uri  string 文件相对路径  如： “a/b/c/PhoneBook.txt”
	 	参三：content   string 要写入文件的内容
	 	参四：fun       fun(path)  完成后的回调函数，回调参数会返回文件保存的完整路径。
	 	示例：
			var path_root = '_doc';
			var path_uri = 'a/b/c/phone_book.txt';
			CreateAndWriteFile(path_root,path_uri,'askdf',function(path){
				var path2 = path.substring(path.indexOf('Android'));
				document.getElementById('Notice').innerHTML = '联系人导出成功！路径：<div>'+path2+'</div>';
			});
	 */
	function CreateAndWriteFile(path_root,path_uri,content,fun){
		plus.io.resolveLocalFileSystemURL(
			path_root,
			function(en){
				en.getFile(
					path_uri,
					{create:true,exclusive:false},
					function(en2){
						if(content){
							en2.createWriter(
								function(w){
									C('文件创建并写入成功！');
									w.write(content);
									w.onwriteend = function(){
										fun(en2.fullPath);
									}
								},
								function(e){
									fun('err');
								}
							);
						}else{
							fun(en2.fullPath);
						}
					},
					function(e){
						fun('err');
					}
				);
			},
			function(e){
				fun('err');
			}
		);
	}
	
	
	
	
	//### plus 准备
	document.addEventListener('plusready', function(){
		
		//导出联系人
		document.getElementById('DownFiles').addEventListener('click',function(){
			var path_root = '_doc';
			var path_uri = 'phone_book.txt';
			CreateAndWriteFile(path_root,path_uri,tmp_str2,function(path){
				var path2 = path.substring(path.indexOf('Android'));
				document.getElementById('Notice').innerHTML = '联系人导出成功！路径：<div>'+path2+'</div>';
			});
		})


		//获取联系人列表
		document.getElementById('GetContactsList').addEventListener('click',function(){
			plus.contacts.getAddressBook(
				//plus.contacts.ADDRESSBOOK_PHONE,
				plus.contacts.ADDRESSBOOK_SIM,
				function(addressbook){
					addressbook.find(
						["displayName","phoneNumbers"],
						function(contacts){
							if(contacts.length < 1){
								return;
							}
							for(var i=0;i<contacts.length;i++){
								var name = contacts[i].displayName;
								for(var j =0;j<contacts[i].phoneNumbers.length;j++){
									var phone = contacts[i].phoneNumbers[j].value;
									phone = phone.replace(/ /g,'');
									tmp_str += "<div style='display:flex'>";
										tmp_str += "<div style='flex:1;text-align:center'>"+ name +"</div>";
										tmp_str += "<div style='flex:1;text-align:center'>" + phone + "</div>";
									tmp_str += "</div>";
									tmp_str2 += name + "\t" + phone + "\r\n";
									
								}
							}
							document.getElementById('list').innerHTML = tmp_str;  //打印到页面上
						}, 
						function (e) {
							C(e.message);
						}
					);
				},
				function(r2){
					console.log(r2);
				}
			);
		})
	});
	
	
	
	function C(r){
		console.log(r);
	}
</script>